package com.haixee.parkdemo.common.context;


import com.haixee.parkdemo.common.entity.AuthData;

/**
 * 请求上下文
 *
 * @author GUIFENG GUO
 * 2020/3/22 9:28
 */
public final class WebContextHolder {


    // ------------------------------ user ------------------------------------
    private static final ThreadLocal<Integer> USER_HOLDER = new ThreadLocal<>();

    public static void setUid(Integer uid) {
        USER_HOLDER.set(uid);
    }

    public static Integer getUid() {
        return USER_HOLDER.get();
    }



    // ------------------------------ acl ------------------------------------

    private static final ThreadLocal<AuthData> AUTH_HOLDER = new ThreadLocal<>();

    public static void setAuth(AuthData authData) {
        AUTH_HOLDER.set(authData);
    }

    public static AuthData getAuth() {
        return AUTH_HOLDER.get();
    }


    // ------------------------------ ip ------------------------------------

    private static final ThreadLocal<String> IP_HOLDER = new ThreadLocal<>();

    public static void setIp(String ip) {
        IP_HOLDER.set(ip);
    }

    public static String getIp() {
        return IP_HOLDER.get();
    }



    // ------------------------------ response ------------------------------------

    private static final ThreadLocal<Integer> RESPONSE_CODE_HOLDER = new ThreadLocal<>();
    private static final ThreadLocal<String> RESPONSE_MSG_HOLDER = new ThreadLocal<>();


    public static void setResponseCode(Integer code) {
        RESPONSE_CODE_HOLDER.set(code);
    }

    public static Integer getResponseCode() {
        return RESPONSE_CODE_HOLDER.get();
    }

    public static void setResponseMsg(String msg) {
        RESPONSE_MSG_HOLDER.set(msg);
    }

    public static String getResponseMsg() {
        return RESPONSE_MSG_HOLDER.get();
    }


    public static void remove() {
        USER_HOLDER.remove();
        IP_HOLDER.remove();
        RESPONSE_CODE_HOLDER.remove();
        RESPONSE_MSG_HOLDER.remove();
        AUTH_HOLDER.remove();
    }
}
